<template>
  <div class="container clearfix">
    <div class="form">
      <div class="form-item">
        <div class="item-name">
          真实姓名
        </div>
        <input type="text"
               v-model="realname"
               class="item-inp"
               placeholder="请输入您的真实姓名">
      </div>
      <picker mode="selector"
              :range="type_arr"
              range-key="type"
              @change="change_type">
        <div class="form-item">
          <div class="item-name">
            证件类型
          </div>
          <input type="text"
                 v-model="type"
                 class="item-inp"
                 maxlength="18"
                 disabled>
          <img :src="base_url+'/jifenshangcheng/xiangqingye/xiayibu@2x.png'"
               v-if="base_url"
               alt=""
               class="icon">
        </div>
      </picker>
    </div>
    <div class="uploaders"
         v-if="type!=''">
      <div class="title">证件照片</div>
      <ul class="list clearfix"
          v-if="base_url&&imgUrl">
        <li class="list-item"
            @tap="select_image('front')">
          <!-- 通行证正面 -->
          <img v-if="type_id!=3"
               :src="img_front==''?base_url+'/mp_2.0/zhengjian/zhengmian@2x.png':imgUrl+img_front"
               mode="widthFix"
               alt=""
               class="img">
          <!-- 护照正面 -->
          <img v-else
               :src="img_front==''?base_url+'/mp_2.0/zhengjian/huzhaofengmian@2x.png':imgUrl+img_front"
               mode="widthFix"
               alt=""
               class="img">
        </li>
        <li class="list-item"
            @tap="select_image('back')">
          <!-- 通行证反面 -->
          <img v-if="type_id!=3"
               :src="img_back==''?base_url+'/mp_2.0/zhengjian/fanmian@2x.png':imgUrl+img_back"
               mode="widthFix"
               alt=""
               class="img">
          <!-- 护照反面 -->
          <img v-else
               :src="img_back==''?base_url+'/mp_2.0/zhengjian/zhengjianye@2x.png':imgUrl+img_back"
               mode="widthFix"
               alt=""
               class="img">
        </li>
      </ul>
    </div>
    <div class="sub-btn"
         @click="identification()">
      提交
    </div>

  </div>
</template>


<script>
import common from '@/assets/js/mmk_common.js'
export default {
  data () {
    return {
      base_url: common.image_resource,
      realname: "",
      img_front: "",
      img_back: "",
      imgUrl: common.image_response,
      type_arr: [
        {
          type: "港澳居民来往内地通行证",
          id: 1
        },
        {
          type: "台湾居民来往大陆通行证",
          id: 2
        },
        {
          type: "护照",
          id: 3
        }
      ],
      type: "",
      type_id: ""
    };
  },
  methods: {
    select_image (type) {
      let that = this;
      wx.chooseImage({
        count: 1,
        sizeType: ["compressed"],
        success (res) {
          wx.showLoading({
            title: "上传中...", //提示的内容,
            mask: true //显示透明蒙层，防止触摸穿透,
          });
          wx.uploadFile({
            url: common.base_url + "api/v4_5/upload", //开发者服务器 url
            filePath: res.tempFilePaths[0], //要上传文件资源的路径
            name: "file", //文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
            formData: {
              dirPath: "authentication",
              upload_status: true
            },
            header: {},
            success: res => {
              wx.hideLoading();
              let result = JSON.parse(res.data);
              wx.showToast({
                title: "图片上传成功", //提示的内容,
                icon: "none", //图标,
                duration: 1500, //延迟时间,
                mask: false //显示透明蒙层，防止触摸穿透
              });
              if (type == "front") {
                that.img_front = result.data.url;
              } else {
                that.img_back = result.data.url;
              }
            },
            fail: () => {
              wx.showToast({
                title: "图片上传失败", //提示的内容,
                icon: "none", //图标,
                duration: 2000, //延迟时间,
                mask: true //显示透明蒙层，防止触摸穿透
              });
            }
          });
        },
        fail () {
          wx.showToast({
            title: "图片选取失败", //提示的内容,
            icon: "none", //图标,
            duration: 2000, //延迟时间,
            mask: true //显示透明蒙层，防止触摸穿透
          });
        }
      });
    },
    identification () {
      if (this.realname.trim() == "") {
        wx.showToast({
          title: "请输入真实姓名", //提示的内容,
          icon: "none", //图标,
          duration: 2000, //延迟时间,
          mask: true //显示透明蒙层，防止触摸穿透
        });
        return false;
      } else if (this.img_front == "") {
        wx.showToast({
          title: "请上传证件正面照片", //提示的内容,
          icon: "none", //图标,
          duration: 2000, //延迟时间,
          mask: true //显示透明蒙层，防止触摸穿透
        });
        return false;
      } else if (this.img_back == "") {
        wx.showToast({
          title: "请上传证件反面照片", //提示的内容,
          icon: "none", //图标,
          duration: 2000, //延迟时间,
          mask: true //显示透明蒙层，防止触摸穿透
        });
        return false;
      } else {
        wx.showLoading({
          title: "提交中...", //提示的内容,
          mask: true //显示透明蒙层，防止触摸穿透,
        });
        common.fly_post("api/v4_5/set_identity", {
          realname: this.realname,
          identity_type: this.type_id,
          identity_img: this.img_front,
          identity_back_img: this.img_back
        }, result => {
          wx.hideLoading();
          let res = result.data;
          if (res.status_code == 0) {
            wx.showToast({
              title: "资料上传成功，请等待审核", //提示的内容,
              icon: "none", //图标,
              duration: 1500, //延迟时间,
              mask: true, //显示透明蒙层，防止触摸穿透
              success () {
                setTimeout(() => {
                  wx.navigateBack({
                    delta: 1 //返回的页面数，如果 delta 大于现有页面数，则返回到首页,
                  });
                }, 1500);
              }
            });
          } else {
            wx.showToast({
              title: res.message,
              icon: "none",
              duration: 2000,
              mask: false
            });
          }
        })
      }
    },
    change_type (e) {
      this.type_id = this.type_arr[e.mp.detail.value].id;
      this.type = this.type_arr[e.mp.detail.value].type;
      this.img_front = "";
      this.img_back = "";
    }
  },
  onLoad () {
    wx.setNavigationBarTitle({
      title: "用户认证"
    });
    Object.assign(this.$data, this.$options.data());
    this.img_front = "";
    this.img_back = "";
  }
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  background: rgba(243, 244, 246, 1);
  box-sizing: border-box;
  .form {
    width: 100%;
    background-color: #fff;
    margin-top: 1px;
    .form-item {
      width: 100%;
      height: 44px;
      display: flex;
      border-bottom: 1px solid rgba(243, 244, 246, 1);
      position: relative;
      .item-name {
        line-height: 44px;
        padding: 0 15px;
        font-size: 14px;
        font-family: "PingFangSC-Regular";
        font-weight: 400;
        color: rgba(51, 51, 51, 1);
      }
      .item-inp {
        flex: 1;
        height: 100%;
        border: none;
        outline: none;
        font-size: 14px;
        font-family: "Regular";
        color: #444;
      }
      .icon {
        display: block;
        width: 20px;
        height: 20px;
        position: absolute;
        right: 5px;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }
  .uploaders {
    background-color: #fff;
    .title {
      line-height: 44px;
      padding-left: 15px;
      font-size: 14px;
      font-family: "PingFangSC-Regular";
      font-weight: 400;
      color: rgba(51, 51, 51, 1);
    }
    .list {
      display: flex;
      justify-content: space-between;
      margin: 0 15px;
      padding-bottom: 15px;
      .list-item {
        width: 46%;
        height: 120px;
        position: relative;
        overflow: hidden;
        box-shadow: 0px 1px 9px 0px rgba(255, 230, 231, 1);
        .img {
          width: 100%;
          display: block;
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
        }
      }
    }
  }
  .sub-btn {
    width: 100%;
    height: 50px;
    line-height: 50px;
    text-align: center;
    color: #fff;
    font-size: 20px;
    background: rgba(255, 56, 62, 1);
    position: absolute;
    bottom: 0px;
  }
}
</style>
